<?php

namespace common\components;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\Module as BaseModule;

/**
 * 基础模块，实现一些额外的初始化操作
 *
 * @package common\components
 */
abstract class Module extends BaseModule
{

    /**
     * @var string 当前模块的识别符号
     */
    public static $identify = '';

    /**
     * @var string 作者名
     */
    public static $author = '';

    /**
     * @var bool 是否是后台应用, 默认为true
     */
    public $isBackend = true;

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();

        if (defined('MODULE_NAMESPACE'))
        {
            $this->controllerNamespace = $this->controllerNamespace . "\\" . MODULE_NAMESPACE;
            $this->viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views/' . MODULE_NAMESPACE;
        }

        $this->registerTranslations();
    }

    /**
     * 注册模块的语言文件
     *
     * @todo  此处应该做缓存
     */
    public function registerTranslations()
    {
        $basePath = $this->getBasePath();

        Yii::$app->i18n->translations['app/' . $this->id . '*'] = [
            'class'          => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath'       => $basePath . DIRECTORY_SEPARATOR . 'messages',
            'fileMap'        => [
                'app/' . $this->id => 'app.php',
            ],
        ];
        Yii::$app->i18n->translations['frontend/' . $this->id . '*'] = [
            'class'          => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath'       => $basePath . DIRECTORY_SEPARATOR . 'messages',
            'fileMap'        => [
                'frontend/' . $this->id => 'frontend.php',
            ],
        ];
        Yii::$app->i18n->translations['backend/' . $this->id . '*'] = [
            'class'          => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath'       => $basePath . DIRECTORY_SEPARATOR . 'messages',
            'fileMap'        => [
                'backend/' . $this->id => 'backend.php',
            ],
        ];
        //var_dump(Yii::$app->i18n->translations);
    }

    /**
     * @param string $category
     * @param string $author
     * @return string
     */
    public static function categoryName($category, $author = null)
    {
        $prefix = $author ? $author : static::$author;
        if ( ! $prefix)
        {
            $prefix = MODULE_NAMESPACE;
        }
        return $prefix . '/' . $category;
    }

    /**
     * 翻译助手方法
     *
     * @param string $category
     * @param string $message
     * @param array  $params
     * @param null   $language
     * @return string
     */
    public static function t($category, $message, $params = [], $language = null)
    {
        //echo self::categoryName($category) . "\n";
        return Yii::t(self::categoryName($category), $message, $params, $language);
    }

    /**
     * 获取当前模型对应的实例
     *
     * @return static
     * @throws \yii\base\InvalidConfigException
     */
    public static function current()
    {
        if ( ! static::$identify)
        {
            throw new InvalidConfigException('You should set $identify at first.');
        }
        return Yii::$app->getModule(static::$identify);
    }
}
